﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace CDude.Vita
{
    internal partial class LanguageSelectForm : Form
    {
        private List<Language> languages = new List<Language>();
        private VCg_MainForm mainform;

        public LanguageSelectForm(VCg_MainForm mainform)
        {
            this.mainform = mainform;
            InitializeComponent();

            if (mainform.CurrentLanguage != null)
                ChangeLanguage(mainform.CurrentLanguage);
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            if (languageList.SelectedNode.Text.StartsWith("Invalid")) return;
            if (languageList.SelectedNode.Index > 0)
            {
                try
                {
                    mainform.ChangeLanguage(Path.Combine(VCg_MainForm.LANGUAGE_DIR, "vclang_" + languages[languageList.SelectedNode.Index - 1].Info.Code + ".xml"));
                }
                catch
                {
                    return;
                }
                mainform.VCConfig.Language = mainform.CurrentLanguage.Info.Code;
            }
            else
                mainform.VCConfig.Language = "default";

            mainform.VCConfig.Save();

            Application.Restart();
        }

        private void ChangeLanguage(Language language)
        {
            this.Text = language.Strings.SelectLanguage;
            applyButton.Text = language.Strings.Apply;
            closeButton.Text = language.Strings.Close;
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void languageList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Text.StartsWith("Invalid"))
            {
                applyButton.Enabled = false;
                languageLabel.Text = "";
                authorLabel.Text = "";
                return;
            }

            applyButton.Enabled = true;

            if (e.Node.Index > 0)
                SetLabels(languages[e.Node.Index - 1]);
            else
            {
                languageLabel.Text = "English";
                authorLabel.Text = "celcodioc";
            }
        }

        private void languageList_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                applyButton_Click(sender, e);
        }

        private void LanuageSelectForm_Load(object sender, EventArgs e)
        {
            List<string> invalidlanguages = new List<string>();

            if (!Directory.Exists(VCg_MainForm.LANGUAGE_DIR)) Directory.CreateDirectory(VCg_MainForm.LANGUAGE_DIR);

            string[] langfiles = Directory.GetFiles(VCg_MainForm.LANGUAGE_DIR, "vclang_*.xml");
            for (int i = 0; i < langfiles.Length; i++)
            {
                string str_lang = Path.GetFileNameWithoutExtension(langfiles[i]).Substring(7);
                try
                {
                    Language lang = new Language(Path.Combine(VCg_MainForm.LANGUAGE_DIR, "vclang_" + str_lang + ".xml"));
                    CultureInfo info = CultureInfo.GetCultureInfo(lang.Info.Code);
                    languages.Add(lang);
                    string displayname = info.DisplayName == info.NativeName ? null : " (" + lang.Info.LLanguage + ")";
                    string englishname = info.DisplayName == info.EnglishName ? null : " (" + lang.Info.Language + ")";
                    languageList.Nodes.Add(str_lang, info.DisplayName + displayname + englishname);
                }
                catch
                {
                    invalidlanguages.Add(str_lang);
                }
            }

            languageList.Sort();
            try
            {
                languages = languages.OrderBy(lang => CultureInfo.GetCultureInfo(lang.Info.Code).DisplayName).ToList();
            }
            catch { }
            languageList.Sorted = false;

            languageList.Nodes.Insert(0, "English");

            for (int i = 0; i < invalidlanguages.Count; i++)
                languageList.Nodes.Add("Invalid language: " + invalidlanguages[i]);

            if (mainform.CurrentLanguage != null)
            {
                languageList.SelectedNode = languageList.Nodes.Find(mainform.CurrentLanguage.Info.Code, false)[0];
                SetLabels(mainform.CurrentLanguage);
            }
            else
            {
                languageLabel.Text = "English";
                authorLabel.Text = "by celcodioc";
            }
        }

        private void SetLabels(Language language)
        {
            if (language.Info.LLanguage != null)
            {
                CultureInfo info = CultureInfo.GetCultureInfo(language.Info.Code);
                languageLabel.Text = info.DisplayName + " (" + language.Info.LLanguage + ")";
                authorLabel.Text = "by " + language.Info.Author;
            }
            else
            {
                languageLabel.Text = null;
                authorLabel.Text = null;
            }
        }
    }
}